package tech.ch.test

import org.apache.spark.sql.{SaveMode, SparkSession}

object Spark2MySQL {

  def main(args: Array[String]): Unit = {

      val spark = SparkSession
                  .builder()
                  .getOrCreate()

      val list = List((5, "ffff"), (6, "hhhh"))

      val rdd = spark.sparkContext.makeRDD(list)

      import spark.implicits._

      val df  = rdd.toDF("id", "name")

    // postgresql://root:123456@localhost:5432/llmops?client_encoding=utf8
      df.write
        .mode(SaveMode.Overwrite)
        .format("jdbc")
        .options(Map(
         "user" -> "root",
          "password" -> "123456",
          "url" -> "jdbc:mysql://192.168.1.8:3306?useUnicode=true",
          "driver" -> "com.mysql.cj.jdbc.Driver",
          "dbtable" -> "test.t_user")
      ).save()


  }

}
